package tps12.axwax.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import tps12.axwax.server.HttpHandler.Result;
import tps12.axwax.server.data.PuzzleStore;
import tps12.axwax.server.util.CompatibleHttpServlet;

@SuppressWarnings("serial")
public class PuzzleServlet extends CompatibleHttpServlet {

	public final static boolean DEVEL = true;
	
	HttpHandler handler = new PuzzleHandler(new PuzzleStore.Factory());
		
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Result res = handler.get(req.getPathInfo());
		resp.setStatus(res.status);
		resp.setContentType(res.type);
		resp.getWriter().println(res.text);
//				
//		resp.setContentType("application/json");
//		if (DEVEL)
//			if (req.getPathInfo().startsWith("/tps12.axwax.AxWax.JUnit/puzzles")) {
//				resp.getWriter().println(new PuzzleServletImpl(new TestPuzzleStoreFactory()).get(req.getPathInfo().replace("/tps12.axwax.AxWax.JUnit/puzzles", "")));
//				return;
//			}
//		resp.getWriter().println(new PuzzleServletImpl(new PMFPuzzleStoreFactory()).get(req.getPathInfo()));
	}
	
	public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Result res = handler.put(req.getPathInfo(), req.getReader());
		resp.setStatus(res.status);
		resp.setContentType(res.type);
		resp.getWriter().println(res.text);

//		if (DEVEL)
//			if (req.getPathInfo().startsWith("/tps12.axwax.AxWax.JUnit/puzzles")) {
//				new PuzzleServletImpl(new TestPuzzleStoreFactory()).put(req.getPathInfo().replace("/tps12.axwax.AxWax.JUnit/puzzles", ""), req.getReader().readLine());
//				return;
//			}
//		new PuzzleServletImpl(new PMFPuzzleStoreFactory()).put(req.getPathInfo(), req.getReader().readLine());
//		resp.setStatus(HttpServletResponse.SC_OK);
	}
	
	BufferedReader getFileUploadReader(HttpServletRequest req) throws IOException {
		ServletFileUpload upload = new ServletFileUpload();
		FileItemIterator iterator = null;
		try {
			iterator = upload.getItemIterator(req);
		} catch (FileUploadException e) {
			return null;
		}
		
		try {
			while (iterator.hasNext()) {
				FileItemStream item = iterator.next();
				if (!item.isFormField())
					return new BufferedReader(new InputStreamReader(item.openStream()));
			}
		} catch (FileUploadException e) { }
		return null;
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		BufferedReader r = getFileUploadReader(req);
		if (r == null) r = req.getReader();
		
		Result res = handler.post(req.getPathInfo(), r);
		resp.setStatus(res.status);
		resp.setContentType(res.type);
		resp.getWriter().println(res.text);
		
//		String result = null;
//		boolean called = false;
//		if (DEVEL)
//			if (req.getPathInfo().startsWith("/tps12.axwax.AxWax.JUnit/puzzles")) {
//				result = new PuzzleServletImpl(new TestPuzzleStoreFactory()).post(req.getPathInfo().replace("/tps12.axwax.AxWax.JUnit/puzzles", ""), r).text;
//				called = true;
//			}
//		if (!called)
//			result = new PuzzleServletImpl(new PMFPuzzleStoreFactory()).post(req.getPathInfo(), r).text;
//		if (result != null) {
//			resp.setStatus(HttpServletResponse.SC_CREATED);
//			resp.addHeader("Location", result);
//			return;
//		}
//		else
//			resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
	}
}
